/**
 * 免费领金币
 */
class AddGoldUI extends UIBase{
    private btn_close:BaseButton;
    private btn_get:BaseButton;
    constructor()
    {
        super('AddGoldView',{showBG:true});
    }

    public init()
    {
        super.init(true);
        let isGetWxAward = RoleCache.instance().isGetWxAward;
        if(isGetWxAward)
        {
            let img:eui.Image = this.btn_get.getChildByName('img_btn') as eui.Image;
            img.source = 'achievement_5';
            ShaderUtil.ImgGray(img);
        }
    }

    public addEvent()
    {
        this.btn_close.addTapHandler(this.close, this);
        this.btn_get.addTapHandler(this.getAward, this);
    }

    protected getAward()
    {
        let isGetWxAward = RoleCache.instance().isGetWxAward;
        if(isGetWxAward)
        {
            MessageUtil.tips('已经领取过了哦~');
            return;
        }
        let showData =   WeChatgGameManager.instance().getOnShowData();
        let isFromMiniApp = false;
        if(showData)
        {
            showData.then((e)=>{
                isFromMiniApp = (e['scene'] == (GameConst.wxScene.MainScene || GameConst.wxScene.DownScene)) || RoleCache.instance().isCanGetWxAward;

                if(isFromMiniApp)
                {
                    let parma = {
                        type:'enter_xcx'
                    }
                    ActivityPrase.instance().getWelfare(parma, this.UpdateUI, this);
                }
                else
                {
                    MessageUtil.tips('请先将小程序添加到我的小程序哦~');
                } 
            })
        }
        
    }

    protected UpdateUI()
    {
        RoleCache.instance().isGetWxAward = true;
        let img:eui.Image = this.btn_get.getChildByName('btn_img') as eui.Image;
        ShaderUtil.ImgGray(img);
        let logic:MainUI = UIManager.instance().getLogicByName(UIConst.MAIN_UI) as MainUI;
        if(logic)
        {
            logic.btn_getGold.redPoint = false;
        }
    }

    protected close()
    {
        this.remove(3);
    }

    public removeEvent()
    {
        this.btn_close.removeTapHandler();
        this.btn_get.removeTapHandler();
    }

    public clear()
    {
        super.clear();
    }
}